package com.hongkzh.www.look.enterprise.userinfo.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.hongkzh.www.R;
import com.hongkzh.www.look.model.bean.AttenedBean;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaCompData;
import com.hongkzh.www.other.utils.SharedPreferencesUtil;
import com.hongkzh.www.other.utils.UIUtils;
import com.hongkzh.www.look.enterprise.userinfo.presenter.MediaComPresenter;
import com.hongkzh.www.view.activity.BaseActivity;
import com.hongkzh.www.look.enterprise.userinfo.view.adapter.InstitutionPagerAdapter;
import com.hongkzh.www.look.enterprise.userinfo.view.customview.CustomViewPager;
import com.hongkzh.www.look.enterprise.userinfo.view.customview.SlidingTabLayout;
import com.hongkzh.www.view.fragment.BaseFragment;
import com.hongkzh.www.look.enterprise.userinfo.view.framgent.MediaIntroduceFragment;
import com.hongkzh.www.look.enterprise.userinfo.view.framgent.MediaPhotosFragment;
import com.hongkzh.www.look.enterprise.userinfo.view.framgent.MediaProductFragment;
import com.hongkzh.www.look.enterprise.userinfo.view.framgent.MediaVideoFragment;
import com.hongkzh.www.look.enterprise.userinfo.view.iview.MediaBaseInfoIView;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 用户简介
 */
public class UserInfoActivity extends BaseActivity<MediaComPresenter> implements MediaBaseInfoIView, View.OnClickListener {

    @BindView(R.id.id_slidingtab_indicator)
    SlidingTabLayout mSlidingtab;
    @BindView(R.id.viewpager_media)
    CustomViewPager mViewPager_media;

    @BindView(R.id.iV_media_top_pic)
    ImageView iVMediaTopPic;
    @BindView(R.id.iV_media_headPic)
    ImageView iVMediaHeadPic;
    @BindView(R.id.tv_media_Name)
    TextView tvMediaName;
    @BindView(R.id.tv_media_introduce)
    TextView tvMediaIntroduce;

    @BindView(R.id.tv_video_Num)
    TextView tvVideoNum;
    @BindView(R.id.tv_fans_Num)
    TextView tvFansNum;
    @BindView(R.id.pageView_Num)
    TextView pageViewNum;

    @BindView(R.id.btn_guanzhu)
    Button btnGuanzhu;
    @BindView(R.id.btn_message)
    Button btnMessage;

    @BindView(R.id.it1_tv)
    TextView it1Tv;

    private String followUserId;
    private boolean isAttened = false;


    MediaIntroduceFragment f1;
    MediaVideoFragment f2;
    MediaPhotosFragment f3;
    MediaProductFragment f4;
    private String uid;
    private String mLoginId;
    private SharedPreferencesUtil spfu;

    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPresenter(new MediaComPresenter());
        ButterKnife.bind(this);
        setTranslucent(R.color.color_D8D8D8);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_userinfo;
    }

    @Override
    protected void init() {
        initView();

    }

    @Override
    protected void setListener() {

        btnGuanzhu.setOnClickListener(this);

    }

    private void initView() {

        Intent intent = getIntent();
        uid = intent.getStringExtra("uid");
        spfu = new SharedPreferencesUtil(UIUtils.getContext());
        mLoginId = spfu.getUserInfo().getLoginUid();

        setPresenter(new MediaComPresenter());
        mSlidingtab.setTabViewLayout(R.layout.layout_tab);

        String[] titles = new String[]{"简介", "视频", "图集", "产品"};
        ArrayList<String> titleArray = new ArrayList<>();
        Collections.addAll(titleArray, titles);


        ArrayList<BaseFragment> fragments = getMediaFragments();
        InstitutionPagerAdapter mPageAdapter = new InstitutionPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager_media.setAdapter(mPageAdapter);
        mViewPager_media.setOffscreenPageLimit(fragments.size() - 1);

        mSlidingtab.setViewPager(mViewPager_media, titleArray.toArray(new String[titleArray.size()]));

        getPresenter().GetMediaComInfo(mLoginId, uid);

    }


    public ArrayList<BaseFragment> getMediaFragments() {
        bundle = new Bundle();
        bundle.putString("uid", uid);
        f1 = new MediaIntroduceFragment();
        f1.setArguments(bundle);
        f2 = new MediaVideoFragment();
        f2.setArguments(bundle);
        f3 = new MediaPhotosFragment();
        f3.setArguments(bundle);
        f4 = new MediaProductFragment();
        f4.setArguments(bundle);

        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(f1);
        fragments.add(f2);
        fragments.add(f3);
        fragments.add(f4);
        return fragments;
    }

    @Override
    public void onShowMediaComInfo(MediaCompData mediaCompData) {
        //TODO: 展示页面
        if (mediaCompData != null) {
            if (null != mediaCompData.getData().getName() && !TextUtils.isEmpty(mediaCompData.getData().getName()))
                //显示企业名字
                tvMediaName.setText(mediaCompData.getData().getName());
            // 企业简介
            tvMediaIntroduce.setText(mediaCompData.getData().getIntroduce());
            // 加载背景图
            Glide.with(this).load(mediaCompData.getData().getBackground()).into(iVMediaTopPic);
            // jiazai 头像
            Glide.with(this).load(mediaCompData.getData().getHeadImg()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(iVMediaHeadPic);

            // 视频总数
            if (null != mediaCompData.getData().getAllCount() + "") {
                tvVideoNum.setText(mediaCompData.getData().getAllCount() + "");
            } else {
                tvVideoNum.setText("0");
            }
            // 粉丝总数
            if (null != mediaCompData.getData().getFollowCount() + "") {
                tvFansNum.setText(mediaCompData.getData().getFollowCount() + "");
            } else {
                tvFansNum.setText("0");
            }
            // 总浏览量
            if (null != mediaCompData.getData().getPlayCount() + "") {
                pageViewNum.setText(mediaCompData.getData().getPlayCount() + "");
            } else {
                pageViewNum.setText("0");
            }

            // 是否已经关注
            if (!TextUtils.isEmpty(mediaCompData.getData().getFollowState())) {
                String flag = mediaCompData.getData().getFollowState();
                if (flag.equals("1")) {
                    btnGuanzhu.setBackground(getResources().getDrawable(R.mipmap.attened));
                } else {
                    btnGuanzhu.setBackground(getResources().getDrawable(R.mipmap.unattened));
                }
            }

        }


    }

    @Override
    public void onShowIfAttend(AttenedBean respone) {
        // 判断当前的状态，显示
        int code;
        if (respone != null) {
            code = respone.getCode();
            if (code == 0) {
                isAttened = true;
                btnGuanzhu.setBackground(getResources().getDrawable(R.mipmap.attened));
            }
        }

    }

    @Override
    public void onShowNoAttend(AttenedBean respone) {
        int code;
        if (respone != null) {
            code = respone.getCode();
            if (code == 0) {
                isAttened = false;
                btnGuanzhu.setBackground(getResources().getDrawable(R.mipmap.unattened));
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_guanzhu:

                if (isAttened) {
                    //如果是已经关注，点击的话，执行取消关注
                    getPresenter().GetFollowDelete(mLoginId, uid);
                } else {
                    getPresenter().GetFollowAdd(mLoginId, uid);
                }

                isAttened = !isAttened;
                break;
        }

    }

    @Override
    public void onError(Exception e) {

    }
}
